#include "../begonia.h"
#include "bullet.h"
#include "scene.h"

bool bullet::isboss()
{
    return false;
}

bullet::bullet()
{
    pre=this;
    next=this;
}

bullet::bullet(double tx,double ty,double tr,double tvx,double tvy,bullet *h)
{
    x=tx;y=ty;r=tr;vx=tvx;vy=tvy;
    c_r=0.0;c_g=1.0;c_b=0.0;bordered=true;
    if(next=h->next) next->pre=this;
    h->next=this;
    pre=h;
}

void bullet::clone(bullet *h)
{
    bullet *a=new bullet(x,y,r,vx,vy,h);
    a->c_r=c_r;
    a->c_g=c_g;
    a->c_b=c_b;
}

bullet::~bullet()
{
    pre->next=next;
    next->pre=pre;
}

void bullet::empty()
{
    while(next!=this) delete next;
}

void bullet::show()
{
    glColor3d(c_r,c_g,c_b);
    drawcircle(x,y,r);
    if(bordered)
    {
        glColor3d(0.0,0.0,0.0);
        drawcircle2(x,y,r);
    }
}

int bullet::incb()
{
        double cbxx[2],cbyy[2];
        //int tx,ty;
        if(x<(cbxx[0]=cbx-cbw/2.0)-r) return 0;
        if(x>(cbxx[1]=cbxx[0]+cbw)+r) return 0;
        if(y<(cbyy[0]=cby-cbh/2.0)-r) return 0;
        if(y>(cbyy[1]=cbyy[0]+cbh)+r) return 0;
        if(x<cbxx[0]) return 1;
        if(x>cbxx[1]) return 1;
        if(y<cbyy[0]) return 1;
        if(y>cbyy[1]) return 1;
        return 2;
        /*
        if(x<cbxx[0]) tx=0;
        else if(x>cbxx[1]) tx=1;
        else return true;
        if(y<cbyy[0]) ty=0;
        else if(y>cbyy[1]) ty=1;
        else return true;
        return sqr(x-cbxx[tx])+sqr(y-cbyy[ty])<=sqr(tr);*/
}

bool bullet::move()
{
    x+=vx*(t-tlast);
    y+=vy*(t-tlast);
    if((vx>0.0?x-r>scw:x+r<0.0)||(vy>0.0?y-r>sch:y+r<0.0)) {delete this;return false;}
    return true;
}

bool bullet::judge()
{
    return sqr(r-corermax)>sqr(x-f.px)+sqr(y-f.py);
}

void bullet::remove()
{
    delete this;
}

void bullet::direction(double dir)
{
} 
void bullet::direction0()
{
        if(vy>1e-9||vy<-1e-9||vx>1e-9||vx<-1e-9) direction(atan2(vy,vx));
}

double bullet::getscore()
{
    return sqr(r)*(sqr(vx)+sqr(vy)+1.0);
}
